create table public.accounts (
  id uuid not null default gen_random_uuid (),
  created_at timestamp with time zone not null default now(),
  email character varying null,
  phone character varying null,
  nick_name character varying null,
  tenant_id character varying null,
  avatar character varying null,
  constraint accounts_pkey primary key (id)
) TABLESPACE pg_default;


create or replace function public.handle_new_user()
returns trigger as $$
declare
  meta jsonb;
begin
  meta := new.raw_user_meta_data;

  insert into public.accounts (
    id,
    created_at,
    email,
    phone,
    tenant_id,
    nick_name,
    avatar
  )
  values (
    new.id,
    now(),
    new.email,
    new.phone,
    meta->>'tenant_id',
    meta->>'nick_name',
    meta->>'avatar'
  );

  return new;
end;
$$ language plpgsql security definer;

create trigger on_auth_user_created
after insert on auth.users
for each row
execute function public.handle_new_user();